import {
  checkLogin
} from './auth.js'
new Vue({
  el: '#app',
  data: {
    IS_LOGIN: null,
    searchKey: '', // 搜索的关键词
    uid: null,
    username: '',
    oldPassword: '',
    password: '',
    password2: ''
  },
  async created() {
    this.IS_LOGIN = await checkLogin()
    if (this.IS_LOGIN == null) {
      location.href = '/web/login.html'
    }
    this.uid = this.IS_LOGIN.uid
    this.username = this.IS_LOGIN.username
  },
  methods: {
    // 修改密码
    async changePassword() {
      if (this.oldPassword == '' || this.password == '' || this.password2 == '') {
        this.$message.error('请填写完整信息');
        return;
      }
      if (this.oldPassword == this.password) {
        this.$message.error('新旧密码不能一致');
        return;
      }
      if (this.password !== this.password2) {
        this.$message.error('两次密码不一致');
        return;
      }
      const res = await request.post('/user/changeUserPassword', {
        uid: this.uid,
        username: this.username,
        oldPassword: this.oldPassword,
        password: this.password,
        password2: this.password2
      })
      if (res.code == 200) {
        this.$message.success('修改成功');
        this.oldPassword = '';
        this.password = '';
        this.password2 = '';
        this.$message.success('正在跳转登录....');
        localStorage.removeItem('token')
        setTimeout(() => {
          location.href = '/web/login.html'
        }, 1500)
      }
    },
    // 查找商品
    searchProduct() {
      console.log('查找商品:');
      location.href = `../web/search.html?searchKey=${this.searchKey}`
    },
    // 退出登录
    logout() {
      localStorage.removeItem('token')
      this.IS_LOGIN = null
      this.$message.success('退出成功')
      location.href = '/'
    },
  }
})